﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Sector7.Framework.Core;
using Sector7.Framework.Utility;
using System.ComponentModel;
using Sector7.Medical.Utility;
using Sector7.Framework.Enums;
using Sector7.Medical.Biz;
using Sector7.Medical.EntityType;

namespace Sector7.Medical.INFM.Windows
{
    /// <summary>
    /// AddUnitWindow.xaml 的交互逻辑
    /// </summary>
    public partial class AddUnitWindow : AddModeWindowBase
    {
        #region Accessor
        /// <summary>
        /// 获取或设置窗口关联单位信息
        /// </summary>
        public M7_Unit Unit
        {
            get { return m_entity as M7_Unit; }
            set { m_entity = value; }
        }
        #endregion

        #region Public functions
        #endregion

        #region Private functions
        protected override void InitializeForCreating()
        {
            txbName.Text = string.Empty;

            m_entity = new M7_Unit();

            base.InitializeForCreating();
        }

        protected override void InitializeForUpdating()
        {
            txbId.Text = Unit.UnitId.ToString();
            txbName.Text = Unit.UnitName;
            cbbType.SelectedIndex = Unit.Flags;

            btnContinue.Visibility = Visibility.Hidden;

            base.InitializeForUpdating();
        }

        protected override bool CheckData()
        {
            m_errors.Clear();

            Unit.UnitName = txbName.Text;
            Unit.Flags = cbbType.SelectedIndex;

            if (string.IsNullOrWhiteSpace(Unit.UnitName))
            {
                m_errors.Add("单位名称", "不能为空！");
            }
            if (Unit.Flags == -1)
            {
                m_errors.Add("单位类型", "不能为空！");
            }

            return base.CheckData();
        }

        protected override void AddData()
        {
            int result = 0;

            switch (m_crud)
            {
                case CrudType.Create:
                    result = Agent_INFS.AddUnit(Unit);
                    if (result > 0)
                    {
                        Unit.UnitId = result;
                    }                    
                    break;
                case CrudType.Update:
                    bool isChanged = false;
                    var delta = GetDelta("UnitId", ref isChanged);
                    if (isChanged)
                    {
                        result = Agent_INFS.UpdateUnit(delta);
                    }
                    break;
                default:
                    break;
            }

            if (result > 0)
            {
                base.AddData();
            }
            else
            {
                Alert.ShowDialog(AlertType.Warning, "操作失败，错误码：[{0}]", result);
            }
        }
        #endregion

        #region Structure
        public AddUnitWindow()
        {
            InitializeComponent();

            FocusedControl = txbName;
        }
        #endregion

        #region Inner members
        #endregion
    }
}
